
using System;
using System.Collections.Generic;
using ScrewTurn.Wiki.PluginFramework;
using System.Web;

namespace ScrewTurn.Wiki {

	/// <summary>
	/// Contains methods for formatting content using a pipeline paradigm.
	/// </summary>
	public static class FormattingPipeline {

		/// <summary>
		/// Performs the Phases 1 and 2 of the formatting process.
		/// </summary>
		/// <param name="raw">The Raw WikiMarkup to format.</param>
		/// <param name="current">The current Page, if any.</param>
		/// <returns>The formatted content.</returns>
		public static string FormatWithPhase1And2(string raw, PageInfo current) {
			PageInfo[] tmp;
			return FormatWithPhase1And2(raw, current, out tmp);
		}

		/// <summary>
		/// Performs the Phases 1 and 2 of the formatting process.
		/// </summary>
		/// <param name="raw">The Raw WikiMarkup to format.</param>
		/// <param name="current">The current Page, if any.</param>
		/// <param name="linkedPages">The Pages linked by the current Page.</param>
		/// <returns>The formatted content.</returns>
		public static string FormatWithPhase1And2(string raw, PageInfo current, out PageInfo[] linkedPages) {
			ContextInformation info = null;
			string username = SessionFacade.Username;
			bool admin = false;
			try {
				admin = SessionFacade.Admin;
			}
			catch {}
			info = new ContextInformation(current, System.Threading.Thread.CurrentThread.CurrentCulture.Name, HttpContext.Current, username, admin);

			// Phase 1
			for(int i = 0; i < Collectors.FormatterProviderCollector.AllProviders.Length; i++) {
				if(Collectors.FormatterProviderCollector.AllProviders[i].PerformPhase1) {
					raw = Collectors.FormatterProviderCollector.AllProviders[i].Format(raw, info, FormattingPhase.Phase1);
				}
			}

			PageInfo[] lnk;

			raw = Formatter.Format(raw, current, out lnk);

			// Phase 2
			for(int i = 0; i < Collectors.FormatterProviderCollector.AllProviders.Length; i++) {
				if(Collectors.FormatterProviderCollector.AllProviders[i].PerformPhase2) {
					raw = Collectors.FormatterProviderCollector.AllProviders[i].Format(raw, info, FormattingPhase.Phase2);
				}
			}

			linkedPages = lnk;

			return raw;
		}

		/// <summary>
		/// Performs the Phase 3 of the formatting process.
		/// </summary>
		/// <param name="raw">The Raw WikiMarkup to format.</param>
		/// <param name="current">The current Page, if any.</param>
		/// <returns>The formatted content.</returns>
		public static string FormatWithPhase3(string raw, PageInfo current) {
			raw = Formatter.FormatPhase3(raw, current);

			ContextInformation info = null;
			string username = SessionFacade.Username;
			bool admin = false;
			try {
				admin = SessionFacade.Admin;
			}
			catch { }
			info = new ContextInformation(current, System.Threading.Thread.CurrentThread.CurrentCulture.Name, HttpContext.Current, username, admin);

			// Phase 3
			for(int i = 0; i < Collectors.FormatterProviderCollector.AllProviders.Length; i++) {
				if(Collectors.FormatterProviderCollector.AllProviders[i].PerformPhase3) {
					raw = Collectors.FormatterProviderCollector.AllProviders[i].Format(raw, info, FormattingPhase.Phase3);
				}
			}

			return raw;
		}

	}

}
